local Player = FindMetaTable("Player")

function Player:ChangeTeam()
	if VAR.MatchStarted then self:ChatPrint(ChosenLang.NoTeamChange) return end
	if self:Team() == TEAM_SPECTATORS then
		if self:CanBecomeWarrior() then
			self:SetTeam(TEAM_WARRIORS)
			for k, v in pairs(ents.FindByClass( "info_player_terrorist" )) do
				if !table.HasValue( VAR.Spawn, k) then
						VAR.Spawn[self:UniqueID()] = k
					break
				end
			end
			self:ChatPrint(ChosenLang.YouBecame .. ChosenLang.Warrior)
		else
			self:ChatPrint(ChosenLang.MaxWarriorsReached)
			return 
		end
	else
		self:SetTeam(TEAM_SPECTATORS)
		VAR.Spawn[self:UniqueID()] = nil
		self:ChatPrint(ChosenLang.YouBecame .. ChosenLang.Spectator)
	end
	self:Spawn()
end

function Player:CanBecomeWarrior()
	local MaxWarriors = Config.MaxWariors or #ents.FindByClass( "info_player_terrorist" )
	if team.NumPlayers(TEAM_WARRIORS) >= MaxWarriors then
		return false
	end
	return true
end